<template>
  <view class="component">
    <u--form
        labelPosition="left"
        :model="item"
        :rules="rules"
        ref="uForm"
        class="form"
        labelWidth="120rpx"
        labelAlign="right"
        :labelStyle="{'font-size': '24rpx', 'padding-right': '20rpx', 'margin-top': '-3rpx'}"
    >
      <u-form-item borderBottom label="父级:" @click="showParent = true; hideKeyboard()">
        <u--input
            v-model="item.parent"
            border="none"
            borderBottom
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
        ></u--input>
        <u-icon slot="right" name="arrow-right"></u-icon>
      </u-form-item>
      <u-form-item borderBottom label="名称:" prop="name">
        <u--input
            v-model="item.name"
            border="none"
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
        ></u--input>
      </u-form-item>
      <u-form-item borderBottom label="排序:" prop="sort">
        <u--input
            v-model="item.sort"
            border="none"
            fontSize="24rpx"
            :customStyle="{'height': '30rpx'}"
        ></u--input>
      </u-form-item>
    </u--form>
    <view class="ops">
      <view class="button">
        <u-button type="primary" text="保存" size="small" @click="update"></u-button>
      </view>
      <view class="button">
        <u-button type="error" text="删除" size="small" @click="remove"></u-button>
      </view>
    </view>
    <u-action-sheet
        :show="showParent"
        :actions="parent"
        title="请选择父级分类"
        @close="showParent = false"
        @select="parentSelect"
    >
    </u-action-sheet>
    <u-toast ref="uToast"></u-toast>
  </view>
</template>

<script>
import Supplier from '../../../api/supplier'
const model = new Supplier()

export default {
  props: {
    item: {
      type: Object,
      default() {
        return {}
      }
    },
    parent: {
      type: Array,
      default() {
        return []
      }
    }
  },
  // 解决小程序样式问题
  options: {
    styleIsolation: 'shared'
  },
  data() {
    return {
      rules: {
        name: {
          type: 'string',
          required: true,
          message: '请填写名称',
          trigger: ['blur', 'change']
        },
        sort: {
          type: 'number',
          // min: 0,
          // max: 200,
          required: true,
          message: '请填写排序, 0 - 200',
          trigger: ['blur', 'change']
        }
      },
      showParent: false
    }
  },
  methods: {
    hideKeyboard() {
      // console.log(this.parent)
      uni.hideKeyboard()
    },
    parentSelect(e) {
      // console.log(e)
      this.item.parent_id = e.id
      this.item.parent = e.name
    },
    update() {
      this.$refs.uForm.validate().then(res => {
        if (res) {
          model.editCategory({ ...this.item }).then(() => {
            this.$refs.uToast.show({
              type: 'success',
              message: '操作成功',
              duration: 500,
              iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png',
              complete: () => {
                this.$emit('submit')
              }
            })
          }).catch(e => {
            this.$refs.uToast.show({
              // type: 'success',
              message: e,
              duration: 500,
            })
          })
        }
      }).catch(e => {
        // console.log(e)
      })
    },
    remove() {
      this.$emit('delete', { id: this.item.id })
    }
  }
}
</script>

<style scoped lang="scss">
// 父级样式
::v-deep {
  .u-action-sheet__item-wrap {
    //background: red;
    max-height: 800rpx;
    overflow-y: auto;
  }
  .u-action-sheet__item-wrap__item {
    padding: 20rpx;
    /* #ifdef MP-WEIXIN */
    padding: 0rpx;
    line-height: 40rpx;
    /* #endif */
    .u-action-sheet__item-wrap__item__name {
      font-size: 24rpx;
    }
  }
}

.component {
  background: white;
  min-height: 300px;
  padding: 20rpx;
  border-radius: 10rpx;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .form {
    //background: red;
  }
  .ops {
    display: flex;
    margin-top: 10px;
    justify-content: center;
    .button {
      //flex: 1;
      padding: 0 10px;
    }
  }
}
</style>
